﻿using System;
using System.Text;

namespace Gadgeteer.Modules.Community.XBee
{
    /// <summary>
    /// When issuing a ND command, the returned message is parsed in to this class
    /// </summary>
    public class NodeDescription
    {
        /// <summary>
        /// The peer device type
        /// </summary>
        public DeviceTypeEnum DeviceType { get; set; }
        /// <summary>
        /// The peer's My address
        /// </summary>
        public ushort MyAddress { get; set; }
        /// <summary>
        /// The peer's serial number MSB
        /// </summary>
        public uint SerialNumberHigh { get; set; }
        /// <summary>
        /// The peer's serial number LSB
        /// </summary>
        public uint SerialNumberLow { get; set; }
        /// <summary>
        /// The peer's Node Identifier
        /// </summary>
        public string NodeIdentifier { get; set; }

        /// <summary>
        /// parses an API message into a node description
        /// </summary>
        /// <param name="message">byte array of API message</param>
        /// <returns>A <see cref="T:Gadgeteer.Modules.Community.XBee.NodeDescription" /> that contains the node description data</returns>
        public static NodeDescription ParseNodeDiscoveryResponse(byte[] message)
        {
            //16-bit MY address
            //First half (SH) of 64-bit address
            //Second half (SL) of 64-bit address
            //Node Identifier text string (this line blank if NI has not been set)
            //Parent Network address (you can ignore this information)
            //Device Type (0=Coordinator, 1=Router, 2=End Device)
            //Status (can be ignored)
            //Profile ID (can be ignored)
            //Manufacturer ID (can be ignored)

            NodeDescription nodeDesc = new NodeDescription();

            byte[] myAddress = new byte[2];
            Array.Copy(message, 8, myAddress, 0, 2);
            nodeDesc.MyAddress = Converter.ToUShort(myAddress);

            byte[] serialNumberHigh = new byte[4];
            Array.Copy(message, 10, serialNumberHigh, 0, 4);
            nodeDesc.SerialNumberHigh = Converter.ToUInt(serialNumberHigh);

            byte[] serialNumberLow = new byte[4];
            Array.Copy(message, 14, serialNumberLow, 0, 4);
            nodeDesc.SerialNumberLow = Converter.ToUInt(serialNumberLow);

            int nodeIdentifierIndex = 18;
            nodeDesc.NodeIdentifier = String.Empty;
            while (message[nodeIdentifierIndex] != 0)
            {
                nodeDesc.NodeIdentifier += (char)message[nodeIdentifierIndex++];
            }
            
            // skip parent network address and get deviceType
            ushort deviceType = (ushort)message[nodeIdentifierIndex + 3];
            nodeDesc.DeviceType = (DeviceTypeEnum)deviceType;

            return nodeDesc;
        }

        /// <summary>
        /// a formated string of the node description
        /// </summary>
        /// <returns>a formated string of the node description</returns>
        public override string ToString()
        {
            return "My Address: " + MyAddress.ToString() + "\n"
                + "Serial Number High: " + SerialNumberHigh.ToString() + "\n"
                + "Serial Number Low: " + SerialNumberLow.ToString() + "\n"
                + "Node Identifier: " + NodeIdentifier + "\n"
                + "Device Type: " + DeviceType.ToString() + "\n";
        }
    }
}
